<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="文件夹名称"
                    prop="folderName">
        <el-input v-model="dataForm.folderName"
                  style="width: 50%;"></el-input>
      </el-form-item>
      <el-form-item label="上级文件夹"
                    prop="fatherId">
        <el-cascader expand-trigger="click"
                     :options="folderList"
                     :props="categoryTreeProps"
                     change-on-select
                     filterable
                     clearable
                     v-model="selectedOptions"
                     @change="handleChange">
        </el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false"
                 size="mini">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()"
                 size="mini">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        folderName: null,
        fatherId: null,
        level: null
      },
      dataRule: {
        folderName: [
          { required: true, message: '文件夹名称不能为空', trigger: 'blur' }
        ]
      },
      folderList: [],
      categoryTreeProps: {
        value: 'id',
        label: 'folderName'
      },
      selectedOptions: []
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.selectedOptions = []
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.$http({
          url: this.$http.adornUrl('/admin/folder/list/0'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          var newData = [];
          data.forEach((v,k,i)=>{
            var ex = false;
            if(v.fatherId != null){
              var fatherId = v.fatherId;
              data.forEach((c,l)=>{
                if(c.id != null && c.id == fatherId){
                  ex = true
                }
              })
            } else {
              ex = true
            }
            if(ex == true){
              newData.push(v)
            }
          })
          this.folderList = treeDataTranslate(newData, 'id', 'fatherId')
        })

        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/admin/folder/getFolderById/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            this.dataForm = data
            this.selectedOptions.push(this.dataForm.fatherId)
            this.categoryTreeProps.id = this.dataForm.id
            this.categoryTreeProps.folderName = this.dataForm.folderName
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl('/admin/folder/update'),
              method: 'put',
              data: this.$http.adornData({
                'id': this.dataForm.id,
                'folderName': this.dataForm.folderName,
                'fatherId': this.dataForm.fatherId
              })
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            this.$http({
              url: this.$http.adornUrl('/admin/folder/save'),
              method: 'post',
              data: this.$http.adornData({
                'folderName': this.dataForm.folderName,
                'fatherId': this.dataForm.fatherId
              })
            }).then(({ data }) => {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    },
    handleChange (val) {
      this.dataForm.fatherId = val[val.length - 1]
    }
  }
}
</script>
